package handel

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"post/api/inits"
	"post/internal/model"
	"post/internal/rep"
	req2 "post/internal/req"
	"post/pkg"
	"strconv"
)

func Upload(c *gin.Context) {
	var req req2.Upload
	c.ShouldBind(&req)
	file, m, err := c.Request.FormFile("image")
	if err != nil {
		return
	}
	upload, err := pkg.Upload(file, m)
	if m.Size >= 2*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "图片大于2mb",
			"data":    nil,
		})
	} else {
		ext := path.Ext(m.Filename)
		if ext != ".png" && ext != ".jpg" {
			c.JSON(http.StatusOK, gin.H{
				"code":    500,
				"message": "图片格式不正确",
				"data":    nil,
			})
		} else {
			if err != nil {
				c.JSON(http.StatusOK, gin.H{
					"code":    500,
					"message": "获取图片链接失败",
					"data":    nil,
				})
			} else {
				c.JSON(http.StatusOK, gin.H{
					"code":    200,
					"message": "获取图片链接成功",
					"data":    upload,
				})
			}
		}
	}

}
func PostAdd(c *gin.Context) {
	var req req2.PostAddReq
	c.ShouldBind(&req)
	var per = ""
	if req.Permission == 1 {
		per = "全部可见"
	}
	if req.Permission == 2 {
		per = "粉丝可见"
	}
	if req.Permission == 3 {
		per = "仅自己可见"
	}
	var pot = model.Pot{
		UserId:     req.UserId,
		Title:      req.Title,
		Content:    req.Content,
		Image:      req.Image,
		Permission: per,
		Follow:     "未关注",
		HotNum:     0,
		LikeNum:    0,
		Review:     "待审核",
	}
	inits.DB.Debug().Create(&pot)
	//创建队列
	bar := pkg.NewRabbitMQSimple("pot")
	marshal, _ := json.Marshal(pot)
	//推送进队列
	bar.PublishSimple(marshal)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "发布成功",
		"data":    pot.ID,
	})
}

func PostList(c *gin.Context) {
	var req req2.PostListReq
	c.ShouldBind(&req)
	var pot []model.Pot
	tx := inits.DB.Debug()
	if req.Type == 1 {
		tx = tx.Where("type = ?", "关注")
	}
	if req.Type == 2 {
		tx = tx.Where("type = ?", "热点")
	}
	if req.Type == 3 {
		tx = tx.Where("type = ?", "最新发布")
	}
	tx.Find(&pot)
	var b []*rep.PotRep
	for _, c := range pot {
		list := rep.PotRep{
			UserId:     c.UserId,
			Title:      c.Title,
			Content:    c.Content,
			Image:      c.Image,
			Permission: c.Permission,
			Follow:     c.Follow,
			HotNum:     c.HotNum,
			LikeNum:    c.LikeNum,
			Review:     c.Review,
			Type:       c.Type,
		}
		b = append(b, &list)
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "列表展示成功",
		"data":    b,
	})
}
func PotShow(c *gin.Context) {
	var req req2.PostShow
	c.ShouldBind(&req)
	var potdata model.PotData
	inits.DB.Debug().Where("id = ?", req.PotId).Find(&potdata)
	var pot = map[string]interface{}{
		"user_id":  potdata.UserId,
		"title":    potdata.Title,
		"content":  potdata.Content,
		"image":    potdata.Image,
		"follow":   potdata.Follow,
		"hot_num":  potdata.HotNum,
		"like_num": potdata.LikeNum,
		"comments": potdata.Comments,
	}
	inits.Rdb.HMSet(inits.Ctx, "PostShow"+strconv.Itoa(req.PotId), pot)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查看详情成功",
		"data":    potdata,
	})
}
